package com.dkd.music.controller;

import com.dkd.common.annotation.Log;
import com.dkd.common.core.controller.BaseController;
import com.dkd.common.core.domain.AjaxResult;
import com.dkd.common.core.page.TableDataInfo;
import com.dkd.common.enums.BusinessType;
import com.dkd.common.utils.poi.ExcelUtil;
import com.dkd.music.domain.Artist;
import com.dkd.music.service.IArtistService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 歌手Controller
 * 
 * @author grz
 * @date 2025-10-26
 */
@RestController
@RequestMapping("/music/artist")
public class ArtistController extends BaseController
{
    @Autowired
    private IArtistService artistService;

    /**
     * 查询歌手列表
     */
    @PreAuthorize("@ss.hasPermi('music:artist:list')")
    @GetMapping("/list")
    public TableDataInfo list(Artist artist)
    {
        startPage();
        List<Artist> list = artistService.selectArtistList(artist);
        return getDataTable(list);
    }

    /**
     * 导出歌手列表
     */
    @PreAuthorize("@ss.hasPermi('music:artist:export')")
    @Log(title = "歌手", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Artist artist)
    {
        List<Artist> list = artistService.selectArtistList(artist);
        ExcelUtil<Artist> util = new ExcelUtil<Artist>(Artist.class);
        util.exportExcel(response, list, "歌手数据");
    }

    /**
     * 获取歌手详细信息
     */
    @PreAuthorize("@ss.hasPermi('music:artist:query')")
    @GetMapping(value = "/{artistId}")
    public AjaxResult getInfo(@PathVariable("artistId") Long artistId)
    {
        return success(artistService.selectArtistByArtistId(artistId));
    }

    /**
     * 新增歌手
     */
    @PreAuthorize("@ss.hasPermi('music:artist:add')")
    @Log(title = "歌手", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Artist artist)
    {
        return toAjax(artistService.insertArtist(artist));
    }

    /**
     * 修改歌手
     */
    @PreAuthorize("@ss.hasPermi('music:artist:edit')")
    @Log(title = "歌手", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Artist artist)
    {
        return toAjax(artistService.updateArtist(artist));
    }

    /**
     * 删除歌手
     */
    @PreAuthorize("@ss.hasPermi('music:artist:remove')")
    @Log(title = "歌手", businessType = BusinessType.DELETE)
	@DeleteMapping("/{artistIds}")
    public AjaxResult remove(@PathVariable Long[] artistIds)
    {
        return toAjax(artistService.deleteArtistByArtistIds(artistIds));
    }
    /**
     * 提供给其他模块进行远程搜索的歌手列表
     * @param artist 查询条件，可以包含 name
     * @return 经过分页的、轻量级的歌手列表
     */
    @GetMapping("/selectList")
    public TableDataInfo selectList(Artist artist)
    {
        // 开启分页，这样无论查出多少数据，都只返回第一页
        startPage();
        // 使用若依生成的 selectArtistList 方法，它天生就支持根据 name 进行模糊查询
        List<Artist> list = artistService.selectArtistList(artist);
        return getDataTable(list);
    }
}
